package com.longrise.android.byjk.plugins.course.coursedetail.healthrisk.catalogue;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.datasource.remote.LoadDataManager;

/**
 * Created by callxkj on 2018/1/15.
 * 健康风险管理师
 */

public class HealthRiskCataloguePresenter extends HealthRiskCatalogueContract.Presenter {

    @Override
    protected void init() {

    }

    /**
     * 获取课程的数据
     */
    @Override
    public void toGetCourseData(String courseid, final boolean isfirst) {
        EntityBean coursEntityBean = new EntityBean();
        coursEntityBean.set("cardno", UserInfor.getInstance().getUsersfzh());
        coursEntityBean.set("courseid", courseid);
        EntityBean argBean = new EntityBean();
        argBean.set("bean", coursEntityBean);
        LoadDataManager.getInstance()
                .callService2(null, UrlConstants.BaseUrl, "FL_getTrainResTreeWithStudyRecords",
                        argBean, new LoadDataManager.OnRequestCallListener2() {
                            @Override
                            public void onSuccess(String key, String service, Object result) {
                                try {
                                    EntityBean bean = (EntityBean) result;
                                    parseResult(bean, isfirst);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                                mView.showNetErrorPage();
                            }


                            @Override
                            public void onFinished(String key, String service) {

                            }
                        });

    }

    private void parseResult(EntityBean bean, boolean isfirst) {
        Integer resultstate = bean.getInt("resultstate");
        if (resultstate == 1) {
            EntityBean resultBean = bean.getBean("result");
            EntityBean latelystudy = resultBean.getBean("latelystudy");
            EntityBean[] catelogs = resultBean.getBeans("catelog");
            String studentno = resultBean.getString("studentno");
            Double totaltraintime = resultBean.getDouble("totaltraintime");
            mView.setCourseData(isfirst, latelystudy, catelogs, studentno, totaltraintime);
        } else {
            String resultdesc = bean.getString("resultdesc");
            mView.showToast(resultdesc);
        }
    }

}
